package com.china08.yxyapi.api.manage.sch.platform.website.section;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SectionMoveListController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @ApiOperation("获取相同类型的一级栏目")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/section/move/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String section2Id = reqModel.section2Id;

        SwsSection swsSection = swsSectionRepository.findById(section2Id);
        Assert.isTrue(null != swsSection, "不存在当前栏目");

        Integer sectionType = swsSection.getSectionType();
        Assert.isTrue(sectionType == 0 || sectionType == 1, "当前栏目没有栏目类型");

        String schoolId = getProfile(request, response).getSchoolId();
        List<SwsSection> list = swsSectionRepository.findBySchoolIdAndPidAndSectionTypeOrderByOrderNumAsc(schoolId, "0", sectionType);

        List<RespModel> returnList = new ArrayList<>();
        for (SwsSection swsSection2 : list) {
            RespModel respModel = new RespModel();
            respModel.id = swsSection2.getId();
            respModel.sectionName = swsSection2.getSectionName();
            returnList.add(respModel);
        }
        return returnList;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String section2Id;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(section2Id), "二级栏目未选定");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String sectionName;
    }
}
